package com.fc.mall.service.impl;

import com.fc.mall.dao.SmsBargainPromotionDao;
import com.fc.mall.dao.SmsBargainPromotionProductDao;
import com.fc.mall.dto.SmsBargainPromotion;
import com.fc.mall.dto.SmsBargainPromotionProduct;
import com.fc.mall.dto.SmsBargainPromotionQueryParam;
import com.fc.mall.exception.ServiceException;
import com.fc.mall.service.SmsBargainPromotionService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description:
 * User: Charles.Gao
 * Date: 2019-09-07 16:06
 */
@Service
public class SmsBargainPromotionServiceImpl implements SmsBargainPromotionService {
    @Autowired
    SmsBargainPromotionDao bargainPromotionDao;

    @Autowired
    SmsBargainPromotionProductDao bargainPromotionProductDao;

    @Override
    public int create(SmsBargainPromotion bargainPromotion) {
        checkTime(bargainPromotion);
        bargainPromotion.setCreateDate(new Date());
        bargainPromotion.setDeleteStatus(0);
        return bargainPromotionDao.insert(bargainPromotion);
    }

    @Override
    public int delete(Long id) {
        return bargainPromotionDao.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Long id, SmsBargainPromotion bargainPromotion) {
        checkTime(bargainPromotion);
        return bargainPromotionDao.updateByPrimaryKey(bargainPromotion);
    }

    @Override
    public List<SmsBargainPromotion> list(String keyword, Integer filterType, Integer pageSize, Integer pageNum) {
        SmsBargainPromotionQueryParam param = new SmsBargainPromotionQueryParam();
        param.setFilterType(filterType);
        param.setKeyword(keyword);
        PageHelper.startPage(pageNum,pageSize);
        return bargainPromotionDao.getList(param);
    }

    @Override
    public SmsBargainPromotion getBargainPromotion(Long id) {
        return bargainPromotionDao.getByPrimaryKey(id);
    }

    @Override
    public List<SmsBargainPromotion> getAllActive() {
        SmsBargainPromotionQueryParam param = new SmsBargainPromotionQueryParam();
        return bargainPromotionDao.getList(param);
    }

    @Override
    public int addProduct(SmsBargainPromotionProduct bargainPromotionProduct) {
        return bargainPromotionProductDao.insert(bargainPromotionProduct);
    }

    @Override
    public int removeProduct(Long id){
        return bargainPromotionProductDao.deleteByPrimaryKey(id);
    }

    @Override
    public int removeProduct(Long pId, Long bId){
        return bargainPromotionProductDao.deleteByPromoIdAndBargainId(pId,bId);
    }

    @Override
    public List<SmsBargainPromotionProduct> getProductList(Long promotionId){
        return bargainPromotionProductDao.getListByPromotionId(promotionId);
    }

    /**
     * 时间验证：结束时间应大于开始时间和当前时间
     * @param bargainPromotion
     */
    private void checkTime(SmsBargainPromotion bargainPromotion){
        if (null != bargainPromotion.getEndDate() && null != bargainPromotion.getStartDate()){
            Long startTime = bargainPromotion.getStartDate().getTime();
            Long endTime = bargainPromotion.getEndDate().getTime();
            Long now = System.currentTimeMillis();
            if (endTime <= startTime || endTime <= now){
                throw new ServiceException("结束时间应大于开始时间和当前时间！");
            }
        }
    }
}
